CFG_MK = $(wildcard cfg.mk)
ifeq ($(CFG_MK),cfg.mk)
include $(CFG_MK)
$(info include cfg.mk file)
else
$(warning not find cfg.mk file)
endif

export CROSS:=
export CC:=$(CROSS)gcc
export AR:=$(CROSS)ar

_BUILD_DATE=$(shell date +%y%m%d%H%M)

CFG_CFLAGS = -DBUILD_DATE=$(_BUILD_DATE) -DVERSION=$(VERSION)

HI_CFLAGS += -Wall -O2 -g $(CFG_CFLAGS) -ffunction-sections -fdata-sections -Wl,--gc-sections 

ifndef PUB_DIR
export PUB_DIR := $(PWD)
endif

export INCLUDE_DIR = $(PUB_DIR)/include



COMMON_FILES = ${PUB_DIR}/common/*

CFG_INC = -I$(PUB_DIR)/common -I$(INCLUDE_DIR)  -Isrc
LIBPATH = -L$(PUB_DIR)/lib  -L/usr/lib/x86_64-linux-gnu


LIB = -Wl,--start-group -lpthread -lrt -ldl  -lm -lz -Wl,--end-group

XLDFLAGS = -Xlinker "-(" $(LIB) -Xlinker "-)"

CC+= -D_GNU_SOURCE $(HI_CFLAGS) $(CFG_CFLAGS)
	
CXX = $(CROSS)-g++
CXX+= -D_GNU_SOURCE $(HI_CFLAGS) $(CFG_CFLAGS)

export LIB LIBPATH CFG_INC COMMON_FILES CXX XLDFLAGS

target:=$(firstword $(MAKECMDGOALS))
    
.PHONY:$(target)

ifeq ($(target), all)
    target=./
endif

ifeq ($(target), clean)
    target=
endif


SRC = $(wildcard *.c)
APP= $(SRC:%.c=%)

OTHERSRC=$(wildcard src/*.c)

all:$(APP)
$(APP): %: %.c
	@$(QUIET) $(CC) -o $@ $< $(OTHERSRC) $(CFG_INC) $(LIBPATH) $(XLDFLAGS)

clean:
	-@rm -f $(APP)
